package com.kx.demo.jdk.collection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author kx
 * @date 2020/4/11
 */
public class KxStack<E> implements List<E> {


    private static final int DEFAULT_CAPACITY = 10;

    private int size = 0;

    private Object[] elements = {};

    private int defaultIncrement = 0;

    public KxStack() {
        elements = new Object[DEFAULT_CAPACITY];
    }

    public KxStack(int capacity) {
        ensureCapacity(capacity);
        elements = new Object[capacity];
    }

    private void ensureCapacity(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException(String.valueOf(capacity));
        }
    }

    private Boolean checkCapacity(int capacity) {
        if (capacity <= 0) {
            return false;
        }
        return true;
    }

    public KxStack(int capacity, int defaultIncrement) {
        ensureCapacity(capacity);
        this.defaultIncrement = defaultIncrement;
        elements = new Object[capacity];
    }

    public E push(E item) {
        ensureCapacityInternal(size + 1);
        elements[size++] = item;
        return item;
    }

    private void ensureCapacityInternal(int minCapacity) {
        if (minCapacity > size) {
            grow(minCapacity);
        }
    }

    private void grow(int minCapacity) {
        int oldCapacity = elements.length;
        int newCapacity = oldCapacity + (defaultIncrement != 0 ? defaultIncrement : oldCapacity);
        if (newCapacity < minCapacity) {
            newCapacity = minCapacity;
        }
        if (newCapacity > (Integer.MAX_VALUE - 8)) {
            newCapacity = Integer.MAX_VALUE;
        }
        elements = Arrays.copyOf(elements, newCapacity);
    }

    public E pop() {
        ensureCapacity(size);
        E item = (E) elements[size - 1];
        elements[--size] = null;
        return item;
    }

    public int search(E item) {
        if (item == null || size <= 0) {
            return -1;
        }
        for (int i = 0; i < size; i++) {
            if (item.equals(elements[i])) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public synchronized int size() {
        return size;
    }

    @Override
    public synchronized boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public synchronized boolean add(E e) {
        return false;
    }

    @Override
    public synchronized boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public synchronized boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public void add(int index, E element) {

    }

    @Override
    public E remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }
}
